//
//  PersonListCell.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/11/27.
//

import Foundation
import UIKit

class PersonListCell: UITableViewCell {
    let selectIV = UIImageView()
    let nameL = UILabel()
    let contentL = UILabel()
    let ctl = UIControl()
    let arrowBtn = UIButton(type: .custom)
    
    open var didSelectItemWith:(() -> ())?
    open var didSelectDetailWith:(() -> ())?

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: .default, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func reload(withModal modal: PersonModal?, isSel: Bool = false, isMutli: Bool = false, userType: String) {
        nameL.text = modal?.nickName
        arrowBtn.isHidden = userType == "00"
        arrowBtn.snp.updateConstraints { (make) -> Void in
            make.width.equalTo(userType == "00" ? 0 : 60)
        }

        if userType == "00" {
            contentL.text = "工号：\(modal?.jobNum ?? "")"
        } else {
            contentL.text = "\(modal?.phonenumber ?? "")"
        }
        
        selectIV.isHidden = !isMutli

        if isMutli {
            if isSel {
                selectIV.image = UIImage(systemName: "checkmark.square.fill")
                selectIV.tintColor = .primary
            } else {
                selectIV.image = UIImage(systemName: "square.fill")
                selectIV.tintColor = .hex("#EDEDED")
            }
            nameL.snp.remakeConstraints { make in
                make.top.equalTo(self.snp.top).offset(10)
                make.left.equalTo(selectIV.snp.right).offset(10)
                make.right.equalToSuperview().offset(-10)
            }
        } else {
            nameL.snp.remakeConstraints { make in
                make.top.equalTo(self.snp.top).offset(10)
                make.left.equalToSuperview().offset(10)
                make.right.equalToSuperview().offset(-10)
            }
        }
    }
    
    @objc func selectTapped() {
        didSelectItemWith?()
    }
    
    @objc func personDetailTapped() {
        didSelectDetailWith?()
    }
    
    func setupUI() {
        selectIV.image = UIImage(systemName: "square.fill")
        addSubview(selectIV)
        selectIV.snp.makeConstraints { (make) -> Void in
            make.centerY.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.width.height.equalTo(20)
        }
        
        nameL.textColor = .hex("#333333")
        nameL.font = .systemFont(ofSize: 17)
        addSubview(nameL)
        nameL.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(10)
            make.left.equalTo(selectIV.snp.right).offset(10)
            make.right.equalToSuperview().offset(-60)
        }
        
        contentL.font = .systemFont(ofSize: 15)
        contentL.textColor = .hex("#999999")
        addSubview(contentL)
        contentL.snp.makeConstraints { make in
            make.top.equalTo(nameL.snp.bottom).offset(6)
            make.left.equalTo(nameL.snp.left)
            make.right.equalTo(nameL.snp.right)
        }
        
        addSubview(line)
        line.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(contentL.snp.bottom).offset(10)
            make.bottom.equalToSuperview()
            make.left.equalToSuperview().offset(10)
            make.right.equalTo(nameL.snp.right)
            make.height.equalTo(0.5)
        }
        
        ctl.addTarget(self, action: #selector(selectTapped), for: .touchUpInside)
        contentView.addSubview(ctl)
        ctl.snp.makeConstraints { make in
            make.top.equalTo(nameL.snp.top)
            make.bottom.equalTo(line.snp.bottom)
            make.left.equalTo(nameL.snp.left)
            make.right.equalTo(nameL.snp.right)
        }
                
        arrowBtn.addTarget(self, action: #selector(personDetailTapped), for: .touchUpInside)
        arrowBtn.setImage(UIImage(systemName: "square.and.pencil"), for: .normal)
        arrowBtn.contentMode = .scaleAspectFit
        arrowBtn.tintColor = .hex("#A5A5A5")
        contentView.addSubview(arrowBtn)
        arrowBtn.snp.makeConstraints { (make) -> Void in
            make.right.equalTo(self.snp.right)
            make.top.equalTo(self.snp.top)
            make.bottom.equalTo(line.snp.bottom)
            make.width.equalTo(60)
        }
    }
    
//    override func setHighlighted(_ highlighted: Bool, animated: Bool) {
//        super.setHighlighted(highlighted, animated: animated)
//        if highlighted {
//            backgroundColor = .hex("#ECECEC")
//        } else {
//            backgroundColor = .white
//        }
//    }
    
    lazy var line: UIView = {
        let li = UIView()
        li.backgroundColor = .hex("#ECECEC")
        return li
    }()
}


